//
//  ToolsViewController.m
//  LOL_JewelBox
//
//  Created by dllo on 15/4/21.
//  Copyright (c) 2015年 dllo. All rights reserved.
//

#import "ToolsViewController.h"
#import "ToolsView.h"
#import "ToolsCell.h"
#import "ToolsModel.h"
#import "ToolsView.h"
#import "DetailTToolsViewController.h"
#import "AFHTTPRequestOperationManager.h"
#import "UIImageView+WebCache.h"
#import "GiftViewController.h"
#import "RunesViewController.h"
#import "AbilityViewController.h"
#import "TeamViewController.h"


@interface ToolsViewController ()<UITableViewDelegate,UITableViewDataSource>

@property(nonatomic, retain)NSMutableArray *modelArr;

@property(nonatomic,retain)UITableView *tableView;

@property(nonatomic, retain)ToolsView *toolsView;

@end

@implementation ToolsViewController

- (void)dealloc
{
    
    [_modelArr release];
    [_tableView release];
    [_toolsView release];
    [_tableView release];
    [super dealloc];
    
}


- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.title = @"百科工具";
        self.modelArr = [NSMutableArray array];
    }
    
    return self;
}

#pragma mark 网络数据解析
- (void)ToolsData
{
    
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    [manager GET:@"http://box.dwstatic.com/apiToolMenu.php?category=database&v=70&OSType=iOS7.1.1&versionName=2.2.1" parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        
        
        NSMutableArray *arr = responseObject;
        for (NSInteger i = 0; i < arr.count; i++) {
            NSMutableDictionary *dic = arr[i];
            ToolsModel *tool = [[ToolsModel alloc]init];
            [tool setValuesForKeysWithDictionary:dic];
            [self.modelArr addObject:tool];
            [tool release];
            
        }
        [self.tableView reloadData];
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        
        NSLog(@"Error: %@", error);
        
    }];
    
}



- (void)viewDidLoad {
    [super viewDidLoad];
     [self ToolsData];
    // 铺设自定义View
    self.toolsView = [[ToolsView alloc] initWithFrame:CGRectMake(0, 0, VIEWWIDTH, VIEWHEIGHT)];
    [self.view addSubview:self.toolsView];
    [_toolsView release];
    
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, VIEWWIDTH, VIEWHEIGHT - 49 - 64) style:UITableViewStylePlain];
    [self.toolsView addSubview:self.tableView];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.rowHeight = (VIEWHEIGHT - 49 - 64) / 5;
    self.tableView.backgroundColor = [UIColor blackColor];
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [_tableView release];
    
    // 装备图片
    UIImageView *detailImage = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"装备.png"]];
    detailImage.frame = CGRectMake(0, 0, (VIEWHEIGHT - 49 - 64) / 5, (VIEWHEIGHT - 49 - 64) / 5);
    [self.tableView addSubview:detailImage];
    [detailImage release];
    
    // 天赋图片
    UIImageView *giftImage = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"天赋.png"]];
    giftImage.frame = CGRectMake(0, (VIEWHEIGHT - 49 - 64) * 1 / 5, (VIEWHEIGHT - 49 - 64) / 5, (VIEWHEIGHT - 49 - 64) / 5);
    [self.tableView addSubview:giftImage];
    [giftImage release];
    
    // 符文图片
    UIImageView *runesImage = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"符文.png"]];
    runesImage.frame = CGRectMake(0, (VIEWHEIGHT - 49 - 64) * 2 / 5, (VIEWHEIGHT - 49 - 64) / 5, (VIEWHEIGHT - 49 - 64) / 5);
    [self.tableView addSubview:runesImage];
    [runesImage release];
    
    // 最佳阵容图片
    UIImageView *abilityImage = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"最佳阵容.png"]];
    abilityImage.frame = CGRectMake(0, (VIEWHEIGHT - 49 - 64) * 3 / 5, (VIEWHEIGHT - 49 - 64) / 5, (VIEWHEIGHT - 49 - 64) / 5);
    [self.tableView addSubview:abilityImage];
    [abilityImage release];
    
    // 召唤师技能
    
    UIImageView *teamImage = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"召唤师技能.png"]];
    teamImage.frame = CGRectMake(0, (VIEWHEIGHT - 49 - 64) * 4/ 5, (VIEWHEIGHT - 49 - 64) / 5, (VIEWHEIGHT - 49 - 64) / 5);
    [self.tableView addSubview:teamImage];
    [teamImage release];
    
    
    
}

// tableView的两个协议方法
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.modelArr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *resue = @"resue";
    ToolsCell *cell = [tableView dequeueReusableCellWithIdentifier:resue];
    if (!cell) {
        cell = [[ToolsCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:resue];
    }
   
    
    ToolsModel *tool = self.modelArr[indexPath.row];
    cell.name.text = tool.name;
    cell.name.textColor = [UIColor whiteColor];
    
    NSURL *url = [NSURL URLWithString:tool.icon];
    [cell.icon sd_setImageWithURL:url];
    cell.backgroundColor = [UIColor blackColor];
    // cell点击后不变色
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    
    // 自定义UITableViewCell选中时背景
//    cell.selectedBackgroundView = [[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"图片.png"]] autorelease];
    
    return cell;
    
}


// 点击事件 跳转详情
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    ToolsModel *tools = self.modelArr[indexPath.row];
    if (indexPath.row == 0) {
        
        DetailTToolsViewController *detail = [[DetailTToolsViewController alloc] init];
        
        detail.tag = tools.tag;
        
        [self.navigationController pushViewController:detail animated:YES];
        [detail release];
        
    }else if (indexPath.row == 1) {
        
        GiftViewController *gift = [[GiftViewController alloc] init];
        
        gift.tag = tools.tag;
        
        [self.navigationController pushViewController:gift animated:YES];
        [gift release];
        
    }else if (indexPath.row == 2) {
        
        RunesViewController *rune = [[RunesViewController alloc] init];
        
        rune.tag = tools.tag;
        
        [self.navigationController pushViewController:rune animated:YES];
        [rune release];
        
    }else if (indexPath.row == 3) {
        TeamViewController *team = [[TeamViewController alloc] init];
        
        team.tag = tools.tag;
        
        [self.navigationController pushViewController:team animated:YES];
        [team release];
        
    }else if (indexPath.row == 4) {
        
        AbilityViewController *ablilty = [[AbilityViewController alloc] init];
        
        ablilty.tag = tools.tag;
        
        [self.navigationController pushViewController:ablilty animated:YES];
        [ablilty release];
    }
    
}








- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
